package ch.zbw.iCoach.Dameon;


import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * This class is unfortunately not implemented, 
 * due to time restrictions the decision was made to upload all languages a the the start of the software. *  
 * Again this class is NOT IMPLEMENTED. 
 * 
 * @author Dameon Crane
 * @version Version 1 (release **.**.****)
 */
public class Browser extends JPanel implements ActionListener {
	
	/**
 	*The constructor calls an instance of BorderLayout via a super call. 
 	*the constructor also call a reference to DatHandlingIO to call the import/export methods
 	*
 	* 
 	* 
 	* @super BorderLayout
 	* @param open this decides whether an open or save screen is diplayed  
 	* 
 	*/	    
	static boolean open;
	private JPanel buttonPanel;
	private DataHandlingIO  io;
	
	static private final String newline = "\n";
	    /**
		*create an instance of JButton, JText and JFileChooser
		*/
	
		JButton openButton, saveButton;
	    JTextArea log;
	    JFileChooser fc;
	    /**
		* This opens a browser screen, depending on the boolean open an open or save screen
		* will be displayed 
		* 
		*/
	 
	    public  Browser(boolean open) {
	        super(new BorderLayout());
	        /**
	        *Create the log first, because the action listeners need to refer to them
	        */
	        log = new JTextArea(5,20);
	        log.setMargin(new Insets(5,5,5,5));
	        log.setEditable(false);
	        JScrollPane logScrollPane = new JScrollPane(log);
	        
	        /**
	        *Create a file chooser
	        */
	        fc = new JFileChooser();
	 
	        /**
	        *Create the open button.  An image is taken from the JLF
	        *Graphics Repository.
	        */
	        
	        if(open == true)  {
	        openButton = new JButton("Open a File...");
	        openButton.addActionListener(this);
	        JPanel buttonPanel = new JPanel(); //use FlowLayout
	        buttonPanel.add(openButton);
	        add(buttonPanel, BorderLayout.PAGE_START);
	        add(logScrollPane, BorderLayout.CENTER);
	        }
	        else{
	        //Create the save button.  
	        saveButton = new JButton("Save a File...");
	        saveButton.addActionListener(this);
	        JPanel buttonPanel = new JPanel(); //use FlowLayout
	        buttonPanel.add(saveButton);
	        add(buttonPanel, BorderLayout.PAGE_START);
	        add(logScrollPane, BorderLayout.CENTER);
	        }
	    }
	 
	    public void actionPerformed(ActionEvent e) {
	    	/**
	    	 * Handle the open button action.
	    	 */
	        
	        if (e.getSource() == openButton) {
	            int returnVal = fc.showOpenDialog(Browser.this);
	 
	            if (returnVal == JFileChooser.APPROVE_OPTION) {
	                File file = fc.getSelectedFile();
	                //This is where a real application would open the file.
	                //io.importLang(CsvPathOS, LangFile, userName)
	                log.append("Opening: " + file.getName() + "." + newline);
	            } else {
	                log.append("Open command cancelled by user." + newline);
	            }
	            log.setCaretPosition(log.getDocument().getLength());
	 
	            /**
		    	 * Handle the save button action.
		    	 */
	        } else if (e.getSource() == saveButton) {
	            int returnVal = fc.showSaveDialog(Browser.this);
	            if (returnVal == JFileChooser.APPROVE_OPTION) {
	                File file = fc.getSelectedFile();
	                //This is where a real application would save the file.
	                log.append("Saving: " + file.getName() + "." + newline);
	            } else {
	                log.append("Save command cancelled by user." + newline);
	            }
	            log.setCaretPosition(log.getDocument().getLength());
	        }
	    }
	 
	    /**
	     * Create the GUI and show it.  For thread safety,
	     * this method should be invoked from the
	     * event dispatch thread.
	     */
	    private static void createAndShowOpen() {
	        //Create and set up the window.
	    	open = true;
	        JFrame frame = new JFrame("Open...");
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 
	        //Add content to the window.
	        frame.add(new Browser(open));
	 
	        //Display the window.
	        frame.pack();
	        frame.setVisible(true);
	    }
	    private static void createAndShowSave() {
	        //Create and set up the window.
	    	open = false;
	        JFrame frame = new JFrame("Save...");
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 
	        //Add content to the window.
	        frame.add(new Browser(open));
	 
	        //Display the window.
	        frame.pack();
	        frame.setVisible(true);
	    }
	 
	}
